package com.gjp.controller;

import com.gjp.bean.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 郭金鹏 on 2018/6/10.
 */
@RestController
public class UserController {
    //id序列
    private volatile Integer Ids = Integer.valueOf(0);
    //数据存储
    private HashMap<Integer, User> userList = new HashMap();

    /**
     * 初始化创建3个对象
     */
    public UserController() {
        User user1 = new User();
        Integer user2 = Ids;
        Integer user3 = Ids = Integer.valueOf(Ids.intValue() + 1);
        user1.setId(Ids);
        user1.setUsername("张三");
        user1.setAge(Integer.valueOf(18));
        user1.setSex("男");
        user1.setLikes(new String[]{"Java", "C++"});
        userList.put(Ids, user1);
        User user21 = new User();
        user3 = Ids;
        Integer var4 = Ids = Integer.valueOf(Ids.intValue() + 1);
        user21.setId(Ids);
        user21.setUsername("小花");
        user21.setAge(Integer.valueOf(16));
        user21.setSex("女");
        user21.setLikes(new String[]{"Vue.js", "HTML"});
        userList.put(Ids, user21);
        User user31 = new User();
        var4 = Ids;
        Integer var5 = Ids = Integer.valueOf(Ids.intValue() + 1);
        user31.setId(Ids);
        user31.setUsername("李四");
        user31.setAge(Integer.valueOf(25));
        user31.setSex("男");
        user31.setLikes(new String[]{"Python", "C#"});
        userList.put(Ids, user31);
    }



    @ApiOperation("自动跳转到API页面")
    @GetMapping({"/"})
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/swagger-ui.html");
    }



    @ApiOperation("根据用户ID查询指定用户信息")
    @ApiImplicitParam(
            name = "id",
            value = "用户ID",
            dataType = "int",
            paramType = "body"
    )
    @GetMapping({"/user/{id}"})
    public User getUser(@PathVariable("id") Integer id) {
        return (User)userList.get(id);
    }



    @ApiOperation("获取所有用户信息")
    @GetMapping({"/userAll"})
    public List<User> getAllUser() {
        ArrayList users = new ArrayList();
        userList.forEach((key, value) -> {
            users.add(value);
        });
        return users;
    }



    /**
     * 注意客户端提交方式，如果是用axios提交请求，提交过来的都是json数据，需要用@RequestBody
     * 来处理json，并且数据格式必须要和java对象一一对应
     * @param user
     * @return
     */
    @ApiOperation("添加用户")
    @PostMapping({"/user"})
    public User addUser(@RequestBody User user) {
        Ids++;
        user.setId(Ids);
        userList.put(Ids, user);
        return (User)userList.get(Ids);
    }



    @ApiOperation("删除用户")
    @ApiImplicitParam(
            name = "id",
            value = "用户ID",
            dataType = "int",
            paramType = "path"
    )
    @DeleteMapping({"/user/{id}"})
    public User deleteUser(@PathVariable("id") Integer id) {
        return (User)userList.remove(id);
    }



    /**
     * 注意客户端提交方式，如果是用axios提交请求，提交过来的都是json数据，需要用@RequestBody
     * 来处理json，并且数据格式必须要和java对象一一对应
     * @param user
     * @return
     */
    @ApiOperation("修改用户")
    @PutMapping({"/user"})
    public User updateUser(@RequestBody User user) {
        System.out.println(user);
        if(user == null || userList.get(user.getId()) == null) {
            return null;
        } else {
            userList.put(user.getId(), user);
            return (User)userList.get(user.getId());
        }
    }
}
